package com.letv.android.client.fragment;

import android.content.Context;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import com.letv.android.client.controller.AlbumGestureObservable;
import com.letv.android.client.view.LetvSeekBar;
import com.letv.android.client.view.ScrollTextView;
import com.letv.android.young.client.R;
import com.letv.core.utils.StatisticsUtils;
import com.letv.core.utils.StringUtils;
import com.letv.core.utils.UIsUtils;
import com.letv.datastatistics.util.PageIdConstant;
import java.util.Observable;

/**
 * 点播半屏播放控制器fragment
 *
 * @author zhuqiao
 */
public class AlbumHalfControllerFragment extends AlbumBaseControllerFragment {

    private boolean mIsHideLayout = true;
    private ImageView mBack;

    /**
     * 全半屏切换
     */
    private OnClickListener mSwitchClick = new OnClickListener() {

        @Override
        public void onClick(View v) {
            mActivity.getController().full();
        }
    };

    public AlbumHalfControllerFragment(Context context, View view) {
        super(context, view);
        initView();
    }

    @Override
    protected void initView() {
        mIsHalf = true;

        mTitleTextView =
                (ScrollTextView) mContentView.findViewById(R.id.player_half_controller_title);
        mPlayImageView = (ImageView) mContentView.findViewById(R.id.player_half_controller_play);
        mSwitchImageView = (ImageView) mContentView.findViewById(R.id.player_half_controller_full);
        mSeekBar = (LetvSeekBar) mContentView.findViewById(R.id.player_half_controller_seekbar);
        //        mTitleDot = mContentView.findViewById(R.id.play_seekbar_skip_begin);
        //        mTrailerDot = mContentView.findViewById(R.id.play_seekbar_skip_end);
        mScreenOrientationLock = (ImageView) mContentView.findViewById(R.id.half_controller_lock);

        mInterceptClickView =
                mContentView.findViewById(R.id.player_half_controller_intercept_click_view);

        super.initView();
        initClick();
    }

    // 加载时不显示播放暂停按钮
    public void showLoad(boolean show) {
        if (show) {
            mPlayImageView.setVisibility(View.GONE);
        } else {
            if (mIsHideLayout) {
                mPlayImageView.setVisibility(View.VISIBLE);
            }
        }
    }

    //亮度提示框
    public void showBrightness() {
        mIsHideLayout = false;
        mPlayImageView.setVisibility(View.GONE);
    }

    //进度提示框
    public void showSeekProgress() {
        mIsHideLayout = false;
        mPlayImageView.setVisibility(View.GONE);
    }

    //声音提示框
    public void showVolume() {
        mIsHideLayout = false;
        mPlayImageView.setVisibility(View.GONE);
    }

    //隐藏提示框
    public void hideLayout() {
        mIsHideLayout = true;
    }

    private void initClick() {
        mSwitchImageView.setOnClickListener(mSwitchClick);
        mBack = (ImageView) mActivity.findViewById(R.id.player_half_controller_back);
        mBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                clickBack();
            }
        });
        mBackForeverView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                clickBack();
            }
        });
    }

    public void clickBack() {
        mUserClickBackTime = StringUtils.timeClockString("yyyyMMdd_HH:mm:ss");
        mUserClickBackStartTime = System.currentTimeMillis();
        mActivity.upToHome();
        mActivity.getController().back();
        // 统计
        StatisticsUtils.staticticsInfoPost(mActivity, "0", "h22", "0005", 1, null, PageIdConstant.halpPlayPage, null, null, null, null, null);
    }

    @Override
    protected void onSeekBarTouchListener(MotionEvent event) {
        super.onSeekBarTouchListener(event);
        if (event.getAction() == MotionEvent.ACTION_UP) {
            // 统计
            StatisticsUtils.staticticsInfoPost(mActivity, "0", "c67", "1001", 4, null, PageIdConstant.halpPlayPage, null, null, null, null, null);
        }
    }

    @Override
    public void initProcess(int max, int progress, int buffer) {
        super.initProcess(max, progress, buffer);
    }

    @Override
    public void hideAllControllerLayout() {
        removeHideHandler();
    }

    @Override
    public void initSeekBarBeginTextView() {
        mSeekBar.initBeginTextView();
    }

    @Override
    public void update(Observable observable, Object data) {
        super.update(observable, data);

        if (data instanceof AlbumGestureObservable.VolumeChangeNotify) {
            delayHide();
        } else if (data instanceof String) {
            String notify = (String) data;
            if (TextUtils.equals(AlbumGestureObservable.ON_CLICK, notify)) {
                // 单击
                if (!UIsUtils.isLandscape(mActivity)) {
                    setControllerVisibility(isVisible() ? View.GONE : View.VISIBLE, true);
                }
            } else if (TextUtils.equals(AlbumGestureObservable.DOUBLE_CLICK, notify)) {
                if (!UIsUtils.isLandscape(mActivity)) {
                    clickPauseOrPlay();
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
    }

    @Override
    protected void clickPauseOrPlay() {
        super.clickPauseOrPlay();
        if (mIsPlaying) {
            // zhuqiao dlna
            mActivity.getController().pause(true);

            StatisticsUtils.staticticsInfoPost(mActivity, "0", "c67", "1000", 2, null, PageIdConstant.halpPlayPage, null, null, null, null, null);
        } else {
            // zhuqiao dlna
            mActivity.getController().start();
            setControllerVisibility(View.VISIBLE, true, Style.HALF);
            StatisticsUtils.staticticsInfoPost(mActivity, "0", "c67", "1004", 1, null, PageIdConstant.halpPlayPage, null, null, null, null, null);
        }
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility != View.VISIBLE) {
            if (!UIsUtils.isLandscape(mActivity)) {
                mBackForeverView.setVisibility(View.GONE);
            }
        }
    }

    public void isHalf(boolean isHalf) {
        if (isHalf) {
            mBack.setVisibility(View.VISIBLE);
        } else {
            mBack.setVisibility(View.INVISIBLE);
        }
    }

}
